<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="所属驻点" prop="keyword">
        <!-- <el-input v-model="queryParams.name" placeholder="请输入角色名称" clearable size="small" style="width: 240px"
                  @keyup.enter.native="handleQuery"/> -->
         <el-select v-model="queryParams.storeId" @change="getList">
          <el-option v-for="(item, i) in storelist" :key='i' :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
       <el-form-item>
        <!-- <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button> -->
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['service:order:create']">新增</el-button>
      </el-col>
     
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="datalist">
      <el-table-column label="订单编号" prop="sn" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="服务时间" prop="appointmentTime" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="车牌号" prop="licensePlate" :show-overflow-tooltip="true" width="100" />
      <el-table-column label="车辆类型" prop="cartType" :show-overflow-tooltip="true" width="80"></el-table-column>
      <el-table-column label="联系人" prop="linkman" :show-overflow-tooltip="true" width="100"></el-table-column>
      <el-table-column label="联系方式" prop="linkPhone" :show-overflow-tooltip="true" width="150"></el-table-column>
      <el-table-column label="服务地址" prop="serviceAddress" :show-overflow-tooltip="true" min-width="150"></el-table-column>
     
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
           <el-button size="mini" type="text" icon="el-icon-edit" @click="handlefp(scope.row)"
                     v-if="scope.row.orderStatus === 0"
                     v-hasPermi="['service:order:fp']">分配订单</el-button>
                      <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCancel(scope.row)"
                      v-if='scope.row.orderStatus - 5 === 0 && !scope.row.refundTime && scope.row.refundAmount'
                     v-hasPermi="['service:order:cancel']">取消并退款</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['service:order:update']">修改</el-button>
         
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['service:order:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择员工" prop="workerId">
           <el-select v-model="form.workerId">
            <el-option v-for="(item, i) in stafflist" :key='i' :label="item.realName" :value="item.id" />
          </el-select>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
orderList,cancelOrder,assignOrder
} from "@/api/service/order";
import {
storeList
} from "@/api/service/shop";
import {
listData
} from "@/api/service/staff";
export default {
  name: "ServicePlan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      datalist: [],
      // 驻点列表
      storelist:[],
      stafflist:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
     
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        storeId:'',
        keyword:''
      },
      // 表单参数
      form: {},
     
      // 表单校验
      rules: {
        workerId: [
          { required: true, message: "人员不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    if(this.$route.query.storeId){
      this.queryParams.storeId = Number(this.$route.query.storeId)
    }
    this.getShopList()
    this.getStaffList()
    setTimeout(() => {
      this.getList()
    }, 50);
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      orderList(this.queryParams).then(
        response => {
          console.log('-------', response, this.queryParams)
          this.datalist = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 查询店铺列表
    getShopList(){
      storeList({pageNo:1, pageSize:999}).then((res) => {
        this.storelist = res.data.records;
      })
    },
    // 查询员工
    getStaffList(){
      listData({pageNo:1, pageSize:999}).then((res) => {
        this.stafflist = res.data.records;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
   
    // 表单重置
    reset() {
     
      this.form = {
        id: undefined,
        workerId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.storeId = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增方案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改角色";
    },
    // 分配订单
    handlefp(row){
        this.reset();
        this.form.id = row.id;
        this.open = true;
        this.title = "分配订单";
    },
    // 取消订单
    handleCancel(row){
      this.$modal.confirm('是否确认取消订单号为"' + row.sn + '"的订单?').then(() => {
          cancelOrder({sn:row.sn}).then((res) => {
            this.$modal.msgSuccess("取消订单成功");
            // this.open = false;
            this.getList()
          })
        }).then(() => {

        })
      
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            // updateRole(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
             assignOrder(this.form).then((res) => {
                 this.$modal.msgSuccess("分配成功");
                 this.open = false;
                 this.getList();
            })
          } else {
            addRole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + ids + '"的数据项?').then(function() {
          return delRole(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   
  }
};
</script>
